<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card>
      <!-- 搜索框区域 -->
      <el-input clearable @clear="searchGoods" placeholder="请输入内容" v-model="searchConent" class="input-with-select">
        <el-button slot="append" icon="el-icon-search" @click="searchGoods"></el-button>
      </el-input>
      <el-button type="primary" @click="goAddPage">添加商品</el-button>
      <!-- 数据表格区域 -->
      <el-table :data="goodsData" style="width: 100%" border>
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column prop="goods_price" label="商品价格" width="80"></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="80"></el-table-column>
        <el-table-column label="创建时间" width="160">
          <template slot-scope="scope">{{scope.row.upd_time | formDate}}</template>
        </el-table-column>
        <el-table-column label="操作" width="120">
          <el-button type="primary" icon="el-icon-edit" size="mini"></el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini"></el-button>
        </el-table-column>
      </el-table>
      <!-- 分页功能 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
    
  </div>
</template>

<script>
import { getAllGoodsListApi } from "../api/index.js";
export default {
  data: function() {
    return {
      // 搜索框输入内容的双向绑定
      searchConent: "",
      // 获取商品列表数据需要的参数
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 10
      },
      // 商品列表数据
      goodsData: [],
      // 商品数据总数
      total: 0
    };
  },
  created() {
    this.getAllGoodsList();
  },
  methods: {
    // 获取所有的商品数据
    async getAllGoodsList() {
      const { data: res } = await getAllGoodsListApi(this.queryInfo);
      if (res.meta.status !== 200) return this.$message.error("数据获取失败");
      this.total = res.data.total
      this.goodsData = res.data.goods;
      // console.log(res);
    },
    // 监听分页的函数
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.queryInfo.pagesize = val
      this.getAllGoodsList()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.queryInfo.pagenum = val
      this.getAllGoodsList()
    },
    // 搜索商品的业务逻辑
    searchGoods() {
      this.queryInfo.query = this.searchConent
      this.getAllGoodsList();
    },
    // 跳转到添加商品界面
    goAddPage() {
      this.$router.push('/goods/add')
    }
  }
};
</script>

<style lang="less" scoped>
.el-input {
  width: 350px;
  margin-right: 20px;
}
</style>

